var DocData={
    serviceId:'',
    baseData:{},
    initBase:function(data){
        var _data = data
        $('input[name=serviceName]').val(_data.serviceName)
        $('input[name=serviceIncome]').val(_data.serviceIncome)
        $('input[name=serviceCost]').val(_data.serviceCost)
        // $('select[name=serviceType]').val(_data.serviceType).trigger('change')
        // $('select[name=serviceLabel]').val(_data.serviceLabel).trigger('change')
        $('img.imghead').attr('src',_data.serviceImg)
        this.imgChange=true
    },
    imgChange:false,
    editURL:'/xrtn-manager/init/gotoServiceSettingAdd',
    baseURL:'/xrtn-manager/init/gotoServiceSetting'
}

$('.zcsearch_input').val($.session.get("serviceName"));

if($('.js_service_edit').length == 1){
    //判断编辑还是新增
    var _upDatas={},editPostUrl='saveServiceInfo',postSuccessTxt='成功新增服务'
    if(baseFn.getQueryString('serviceId') != null){
        DocData.serviceId = baseFn.getQueryString('serviceId')
        $('.js_line_last').text('编辑服务')
        _upDatas.serviceId=DocData.serviceId
        editPostUrl = 'updateServiceInfo',
        postSuccessTxt='服务修改成功';
        
    }
    //编辑
    if(DocData.serviceId != ''){
        $.get(baseUrl.base+baseUrl.init+'selectServiceInfo',_upDatas,function(res){
            if(res.code == 0){
                DocData.baseData = res.results
                DocData.initBase(res.results)
                $('input[name=imgurl]').val(res.results.serviceImg)
            }
        },'json')
    }


    /*组件初始化*/
   
    //图片上传
    $('.imghead').on('click',function(){
        $('#Js_previewImgNew').trigger('click')
    })
    function zcuploadImageCover(e){
        baseFn.imgUpload($(e).parents('form').attr('id'),function(res){
            $(e).siblings('.Js_preview').find('img').attr('src',res.url)
            DocData.imgChange=true
            $('input[name=imgurl]').val(res.url)
        })
    }
    //验证
    var myValidform = $(".js_service_edit").Validform({
        tiptype:2,
        datatype:{
            putso:function(gets,obj,curform,regxp){
                /*参数gets是获取到的表单元素值，
                  obj为当前表单元素，
                  curform为当前验证的表单，
                  regxp为内置的一些正则表达式的引用。*/
                var _checkpwrap = obj.parents('.checkpwrap')
                var _type = _checkpwrap.find('.checkp').val()
                var _userid = _checkpwrap.find('.userid').val()
                var _post = _checkpwrap.find('.post').val()
                var reg1=regxp["*"],
                    reg2=/[\d]{7}/,
                    mobile=curform.find(".mobile");
                if(obj){
                    if(_type == '1'){
                        //人
                        //console.log(_userid)
                        if(reg1.test(_userid)){return true;}
                    }else if(_type == '3'){
                        //岗位
                        if(reg1.test(_post)){return true;}
                    }
                }
                return false;
            },
            ztreegroup:function(gets,obj,curform,regxp){
                //console.log(gets)
                if(gets == ''){
                    return false
                }else{
                }
            },
            select2:function(gets,obj,curform,regxp){
                if(gets.length ==0){
                    obj.siblings('.select2').find('.select2-selection').css('borderColor','red')
                    return false
                }else{
                    obj.siblings('.select2').find('.select2-selection').css('borderColor','#ccc')
                }
            }
        }
    }); 
    /*组件初始化*/
    $('.js_service_edit .edit_ok').on('click',function(){
        if(!myValidform.check()){
            return false
        }
        _upDatas.serviceName=$('input[name=serviceName]').val();
        _upDatas.serviceIncome=$('input[name=serviceIncome]').val();
        _upDatas.serviceCost=$('input[name=serviceCost]').val();
        if(DocData.imgChange){
            _upDatas.serviceImg=$('img.imghead').attr('src')
        }else{
            baseFn.dialog({
                title:'失败',
                content:'请传图片'
            })
        }
        // return false
        $.post(baseUrl.base+baseUrl.init+editPostUrl,_upDatas,function(res){
            if(res.code == '0'){
                DocData.serviceId=res.results
                baseFn.infoDialog(postSuccessTxt)
            }else{
                baseFn.infoDialog(res.info)
            }
        },'json')
    }) 
    $('.js_service_edit .edit_up').on('click',function(){
        if(!myValidform.check()){
            return false
        }
        var _upDatas={
            serviceId:DocData.serviceId,
            serviceStatus:1
        }
        baseFn.dialog({
            title:'上架服务',
            content:'确定上架该服务么？',
            ok:function(){
                $.post(baseUrl.base+baseUrl.init+'updateServiceInfo',_upDatas,function(res){
                    if(res.code == '0'){
                        baseFn.infoDialog('上架成功')
                    }else{
                        baseFn.infoDialog(res.info)
                    }
                },'json')
            },
            cancel:true
        })
        
    })       
}

var orderListDTdata = {
    serviceName: $.session.get("serviceName"),
}

if($('.service_list').length != 0){
    //推拿服务列表
    var service = baseFn.rpDataTables('.service_list',{
        ajUrl:baseUrl.base+baseUrl.init+'selectInitServiceList',
        ajData: orderListDTdata,
        ajDataSrc:"results",
        ajType:'get',
        sessionPageKey: "servicePage",
        scrollY:windowOffsetData.tableHeight(),
        autoWidth:false,
        columns:[
            { "data": "serviceName",width:"30%",defaultContent:'',className:'imgpwrap img2p',render:function(data,type,row, meta){
                var _img=baseUrl.tempHeadImg
                if(baseFn.notNull(row.serviceImg)){
                    _img = row.serviceImg
                }
                // return '<img class="bespeak_change_img" src="'+row.serviceImg+'">\
                return '<div class="datatable_img_div" data-vals="'+_img+'" style="background-image:url('+_img+')"></div>\
                        <div class="pwrap">\
                            <span>'+data+'</span>\
                            <span>'+row.serviceCost+'</span>\
                        </div>'
            }},
            { "data": "orderNumber",width:"50%",defaultContent:''},
            { "data": 'serviceStatus',defaultContent:'无',width:"15%",render:function(data,type,row, meta){
                if(data == 1){
                    return '<a href="javascript:void(0)" class="datatablebtn datatablebtnRed remove" data-vals="'+row.serviceId+'">下架</a>';
                    
                }else{
                    return '<a href="javascript:void(0)" class="datatablebtn datatablebtnRed up" data-vals="'+row.serviceId+'">上架</a> <a href="javascript:void(0)" class="datatablebtn edit" data-vals="'+row.serviceId+'">编辑</a>';
                }
                
            }}
        ],
        initComplete:function(){
            
            
        },
        page:function () {

        },
        drawCallback: function( settings ) {
            $('.remove').off('click').on('click',function(){
                //权限管理 59 服务的下架/上架/编辑操作
                if(baseFn.getPower(59)){
                    baseFn.infoDialog('您没有权限使用功能！')
                    return false
                }
                var _id = $(this).data('vals')
                var _tr = $(this).parents('tr')
                var _upDatas = {
                    serviceId:_id,
                    serviceStatus:0
                }
                baseFn.dialog({
                    title:'下架服务',
                    content:'确定下架该服务么？',
                    ok:function(){
                        $.post(baseUrl.base+baseUrl.init+'updateServiceInfo',_upDatas,function(res){
                            var _txt = '下架成功'
                            if(res.code == 0){
                                _txt = '下架成功'
                                var _d = service.row(_tr).data()
                                _d.serviceStatus=0
                                service.row(_tr).data(_d).draw(false)
                            }else{
                                _txt = res.info
                            }
                            baseFn.infoDialog(_txt)
                        },'json')
                    },
                    cancel:true
                })
            })
            $('.up').off('click').on('click',function(){
                //权限管理 59 服务的下架/上架/编辑操作
                if(baseFn.getPower(59)){
                    baseFn.infoDialog('您没有权限使用功能！')
                    return false
                }
                var _id = $(this).data('vals')
                var _tr = $(this).parents('tr')
                var _upDatas = {
                    serviceId:_id,
                    serviceStatus:1
                }
                baseFn.dialog({
                    title:'上架服务',
                    content:'确定上架该服务么？',
                    ok:function(){
                        $.post(baseUrl.base+baseUrl.init+'updateServiceInfo',_upDatas,function(res){
                            var _txt = '上架成功'
                            if(res.code == 0){
                                _txt = '上架成功';
                                var _d = service.row(_tr).data()
                                _d.serviceStatus=1
                                service.row(_tr).data(_d).draw(false)
                            }else{
                                _txt = res.info
                            }
                            baseFn.infoDialog(_txt)
                        },'json')
                    },
                    cancel:true
                })
            })
            $('.edit').off('click').on('click',function(){
                //权限管理 59 服务的下架/上架/编辑操作
                if(baseFn.getPower(59)){
                    baseFn.infoDialog('您没有权限使用功能！')
                    return false
                }
                var _id = $(this).data('vals')
                window.location.href=DocData.editURL+"?serviceId="+_id
            })
            //图片初始化
            $('.datatable_img_div').each(function(ids,val){
                baseFn.imgSize({
                    src: $(val).data('vals'),
                    hFn: function(res) {
                        res.dom.css({'background-size':'auto 100%'})           
                    },
                    wFn: function(res) {
                        res.dom.css({'background-size':'100% auto'}) 
                    },
                    ratio: 1,
                    otherData: {
                        dom:$(val)
                    }
                })
            })
        }
    })

    $('.zcsearch_btn').on('click',function(val){
        var _txt =$.trim($(this).siblings('input.zcsearch_input').val())
        $.session.set("serviceName",_txt);
        service.settings()[0].ajax.data={
            serviceName:_txt
        }
        service.ajax.reload()
    })
    $("input.zcsearch_input").keydown(function(event) {
        if (event.keyCode == "13") {
            var _txt =$.trim($(this).val())
            $.session.set("serviceName",_txt);
            service.settings()[0].ajax.data={
                serviceName:_txt
            }
            service.ajax.reload()
        }
    });
}
//表格自适应
window.onresize = function(){
    if($('.service_list').length != 0){        
        windowOffsetData.refresh();
        $(service.table().body()).parents('.dataTables_scrollBody').css('max-height',windowOffsetData.tableHeight());
        service.draw(false);
    }
}


